<template>
  <div class="app-container">
    <div class="app-container">
      <el-button type="success" @click="clear">清除地址缓存</el-button>
      <span class="a">修改(或者添加,删除)地址后, 请点击清除缓存 | 同时前端也需要清除缓存(默认24小时自动清除)</span>
    </div>
    <el-tree
      empty-text="加载中,请稍等......."
      :data="list"
      node-key="code"
      lazy
      :default-expanded-keys="defaultExpanded"
      ref="tree"
      :load="load" >
      <div class="custom-tree-node" slot-scope="{ node, data }">
        <span>
          <el-input v-model="data.name" />
        </span>
        <span>
          <el-button
            size="mini"
            type="primary"
            @click.stop="() => save(data)">
            保存
          </el-button>
          <el-button
            size="mini"
            type="warning"
            @click.stop="() => remove(data, node)">
            删除
          </el-button>
          <el-button
            size="mini"
            type="success"
            @click.stop="() => create(data, node)">
            添加子项
          </el-button>
        </span>
      </div>
    </el-tree>
    <el-dialog
      title="填写地区信息"
      :visible.sync="dialogVisible"
      append-to-body
      width="30%">
      <span>
        <el-input v-model="name" placeholder="请输入名称" />
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from '@/api'

export default {
  name: 'SystemAddress',
  data() {
    return {
      list: [],
      dialogVisible: false,
      name: '',
      obj: {},
      defaultExpanded: []
    }
  },
  mounted() {
  },
  methods: {
    load(node, resolve) {
      // console.log(node, resolve)
      if (node.level === 0) {
        this.getMain(resolve)
      }
      // console.log(node.key)
      this.getData(node.key, resolve)
    },
    async getMain(resolve) {
      this.list = []
      api.system.addressMain().then(response => {
        resolve(response.data.list)
      })
    },
    async getData(id, resolve) {
      api.system.addressGet({ id }).then(response => {
        resolve(response.data.list)
      })
    },
    // 保存
    save(data) {
      api.system.addressSave({ ...data }).then(() => {
        this.$message.success('保存成功')
      })
    },
    remove(data, node) {
      api.system.addressRemove({ ...data }).then(() => {
        this.$message.success('删除成功')
        const p = node.parent
        if (p && p.childNodes) {
          p.childNodes = p.childNodes.filter(x => x.id !== node.id)
        }
      })
    },
    create(data, node) {
      this.dialogVisible = true
      this.obj = {
        data,
        node
      }
      // console.log(this.dialogVisible)
      // api.system.addressRemove({ ...data }).then(() => {
      //   this.$message.success('删除成功')
      //   const p = node.parent
      //   if (p && p.childNodes) {
      //     p.childNodes = p.childNodes.filter(x => x.id !== node.id)
      //   }
      // })
    },
    saveAdd() {
      api.system.addressAdd({ ...this.obj.data, name: this.name }).then(response => {
        this.$message.success('添加成功')
        this.dialogVisible = false
        // 添加到节点
        this.$refs.tree.append(response.data, this.obj.node)
        // 清空
        this.name = ''
      })
    },
    // 清除缓存
    clear() {
      api.system.addressClear().then(() => {
        this.$message.success('清除成功')
      })
    }
  }
}
</script>

<style scoped lang="scss">
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 10px;

}

.el-tree ::v-deep.el-tree-node > .el-tree-node__content {
  //width: 264px;
  height: 40px;
  //background: red;
  margin-bottom: 5px;
  border-bottom: 1px solid #f3f5f7;
}

.a {
  font-size: 12px;
  margin-left: 10px;
  color: red;
}
</style>

